log using DO_RA_IMFCONDBELOW , replace
/**********************/
/*Clean and set memory*/
/**********************/
clear all
set matsize 5000
set more off

/******************/
/*Install programs*/
/******************/
/*
cap ssc install xtabond2 
cap ssc install xtivreg2 
cap ssc install spmat 
cap ssc install spmack
*/
/*********************/
/*Sets base directory*/
/*********************/
global project "D:\IMF_INE_2_2021"  /* Set base directory                         */

local  repsBS=100                                                                           /* Number of bootstrap repetitions            */

/****************************/
/*Create anxillary variables*/
/****************************/

use "$project/imf_ehii_CONDITIONS_code.dta"
xtset ccode1 year
rename ehii y 

/*Drop high-income countries*/
gen high=.
replace high=1 if cname=="Andorra"
replace high=1 if cname=="Antigua and Barbuda"
replace high=1 if cname=="Aruba"
replace high=1 if cname=="Australia"
replace high=1 if cname=="Austria"
replace high=1 if cname=="Bahamas, The"
replace high=1 if cname=="Bahrain"
replace high=1 if cname=="Barbados"
replace high=1 if cname=="Belgium"
replace high=1 if cname=="Bermuda"
replace high=1 if cname=="British Virgin Islands"
replace high=1 if cname=="Brunei Darussalam"
replace high=1 if cname=="Canada"
replace high=1 if cname=="Cayman Islands"
replace high=1 if cname=="Channel Islands"
replace high=1 if cname=="Chile"
replace high=1 if cname=="Croatia"
replace high=1 if cname=="Curacao"
replace high=1 if cname=="Cyprus"
replace high=1 if cname=="Czech Republic"
replace high=1 if cname=="Denmark"
replace high=1 if cname=="Estonia"
replace high=1 if cname=="Faroe Islands"
replace high=1 if cname=="Finland"
replace high=1 if cname=="France"
replace high=1 if cname=="French Polynesia"
replace high=1 if cname=="Germany"
replace high=1 if cname=="Gibraltar"
replace high=1 if cname=="Greece"
replace high=1 if cname=="Greenland"
replace high=1 if cname=="Guam"
replace high=1 if cname=="Hong Kong SAR, China"
replace high=1 if cname=="Hungary"
replace high=1 if cname=="Iceland"
replace high=1 if cname=="Ireland"
replace high=1 if cname=="Isle of Man"
replace high=1 if cname=="Israel"
replace high=1 if cname=="Italy"
replace high=1 if cname=="Japan"
replace high=1 if cname=="Korea, Rep."
replace high=1 if cname=="Kuwait"
replace high=1 if cname=="Latvia"
replace high=1 if cname=="Liechtenstein"
replace high=1 if cname=="Lithuania"
replace high=1 if cname=="Luxembourg"
replace high=1 if cname=="Macao SAR, China"
replace high=1 if cname=="Malta"
replace high=1 if cname=="Monaco"
replace high=1 if cname=="Nauru"
replace high=1 if cname=="Netherlands"
replace high=1 if cname=="New Caledonia"
replace high=1 if cname=="New Zealand"
replace high=1 if cname=="Northern Mariana Islands"
replace high=1 if cname=="Norway"
replace high=1 if cname=="Oman"
replace high=1 if cname=="Palau"
replace high=1 if cname=="Poland"
replace high=1 if cname=="Portugal"
replace high=1 if cname=="Puerto Rico"
replace high=1 if cname=="Qatar"
replace high=1 if cname=="San Marino"
replace high=1 if cname=="Saudi Arabia"
replace high=1 if cname=="Seychelles"
replace high=1 if cname=="Singapore"
replace high=1 if cname=="Sint Maarten (Dutch part)"
replace high=1 if cname=="Slovak Republic"
replace high=1 if cname=="Slovenia"
replace high=1 if cname=="Spain"
replace high=1 if cname=="St. Kitts and Nevis"
replace high=1 if cname=="St. Martin (French part)"
replace high=1 if cname=="Sweden"
replace high=1 if cname=="Switzerland"
replace high=1 if cname=="Trinidad and Tobago"
replace high=1 if cname=="Turks and Caicos Islands"
replace high=1 if cname=="United Arab Emirates"
replace high=1 if cname=="United Kingdom"
replace high=1 if cname=="United States"
replace high=1 if cname=="Uruguay"
replace high=1 if cname=="Virgin Islands (U.S.)"

drop if high==1


/* Create IMF program participation variable based on IMF binding conditions (below mean [=8.198454]) */
drop if imf_all==.
drop if BA2TOT==.
gen imf_all_con=0
replace imf_all_con=1 if imf_all==1 & BA2TOT<8.198454
/* Identifies transitions: IMF program participation with binding conditions (below mean [=8.198454]) */
gen timfprogram=.
replace timfprogram=1 if imf_all_con==1&l.imf_all_con==0
replace timfprogram=0 if imf_all_con==0&l.imf_all_con==0 

/* Defines lags of inequality */
gen lag1y=l1.y
gen lag2y=l2.y
gen lag3y=l3.y
gen lag4y=l4.y                       

/* Drop observations with no data to estimate probit */
keep if lag1y!=. 
keep if lag2y!=.
keep if lag3y!=.
keep if lag4y!=.                     

/* Creates lags of inequality */


gen ydep1=L5.y-L.y
gen ydep2=L4.y-L.y
gen ydep3=L3.y-L.y
gen ydep4=L2.y-L.y
gen ydep5=0                        

/* Creates leads of inequality */
gen ydep6=y-L.y
gen ydep7=F1.y-L.y
gen ydep8=F2.y-L.y
gen ydep9=F3.y-L.y
gen ydep10=F4.y-L.y
gen ydep11=F5.y-L.y
  gen ydep12=F6.y-L.y
  gen ydep13=F7.y-L.y
  gen ydep14=F8.y-L.y
  gen ydep15=F9.y-L.y

/* Define sample */
keep if timfprogram!=.	

/* Set seed */				 
set seed 12345        
                          
                        
                      

/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/*********************DEFINE PROGRAM THAT COMPUTES THE REGRESSION ADJUSTMENT ESTIMATOR FOR ALL YEARS  **************************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
capture program tefpas_ra, eclass

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: tab year if ydep1!=. & timfprogram!=., gen(dumyears)

/* Estimates for year -5 */
cap: teffects ra (ydep1 lag1y lag2y lag3y lag4y dumyears*, noconstant) (timfprogram), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=_b[ATET: r1vs0.timfprogram]
}
else{
matrix b=(.)
}
quietly: drop dumyears*



/* Estimates for years -4, -3, -2, -1 are zero by construction */
matrix b=(b, 0)
matrix b=(b, 0)
matrix b=(b, 0)
matrix b=(b, 0)

/* Estimates for years 0 to 10 */
forvalues s=6(1)15{

/* Create year dummies and avoids using ones with no observation (program teffects gets stuck with variables it should drop) */
quietly: tab year if ydep`s'!=. & timfprogram!=., gen(dumyears)

/* Estimates for year s */
cap: teffects ra (ydep`s' lag1y lag2y lag3y lag4y dumyears*, noconstant) (timfprogram), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.timfprogram])
}
else{
matrix b=(b, .)
}
quietly: drop dumyears*
}

/* Post results in matrix b, variance computed by bootstrapping later */
ereturn post b
end





/*******************************************************************************************************************************/
/*******************************************************************************************************************************/
/************************************ ESTIMATION PROCEDURES AND STORING RESULTS ************************************************/
/*******************************************************************************************************************************/
/*******************************************************************************************************************************/

/* 1. Bootstrap and save results */
xtset, clear
bootstrap _b, reps(`repsBS') cluster(ccode1): tefpas_ra 
parmest, format(estimate min95 max95) saving("$project\resultsIMFCONDBELOW\impulse_ra_alt", replace)

/* Results, 5-1 years before transition */
estimates store e0
nlcom (effect0: (_b[c1]+_b[c2]+_b[c3]+_b[c4]+_b[c5])/5), post
estimates store e1
/* Results, 1-5 years after transition */
estimates restore e0
nlcom (effect0: (_b[c6]+_b[c7]+_b[c8]+_b[c9]+_b[c10])/5), post
estimates store e2

/* Results, 6-11 years after transition */
estimates restore e0
nlcom (effect0: (_b[c11]+_b[c12]+_b[c13]+_b[c14]+_b[c15])/5), post
estimates store e3

log close

 translate DO_RA_IMFCONDBELOW.smcl DO_RA_IMFCONDBELOW.pdf